/*
 * Reference.java
 *
 * Created on July 31, 2006, 10:45 AM
 *
 * (C) R. Alexander Milowski alex@milowski.com
 */

package com.smallx.xproc;

import org.infoset.xml.Name;

/**
 *
 * @author R. Alexander Milowski
 */
public class Reference {

   static class ParseError extends Exception {
      ParseError(String msg) {
         super(msg);
      }
   }

   protected Reference() {
   
   }
   
   /*
   enum Axis {
      Step,
      Port,
      Parameter
   }
   
   Axis axis;
   String name;
   
   Reference child;
   
   public Reference(Axis axis,String name) {
      this.axis = axis;
      this.name = name;
      this.child = child;
   }
   
   public String getName() {
      return name;
   }
   
   public Axis getAxis() {
      return axis;
   }
   
   public Reference getNext() {
      return child;
   }
   
   public static Reference parse(String expr) 
      throws ParseError
   {
      return parse(Axis.Step,expr);
   }
   
   private static Reference parse(Axis defaultAxis,String expr)
      throws ParseError
   {
      int slash = expr.indexOf('/');
      if (slash==0) {
         throw new ParseError("No step before '/'.");
      }
      String stepExpr = slash<0 ? expr : expr.substring(0,slash);
      int axisSeparator = stepExpr.indexOf("::");
      if (axisSeparator==0) {
         throw new ParseError("No axis name before '::'.");
      }
      
      Axis axis = defaultAxis;
      String nameString = stepExpr;
      
      if (axisSeparator>0) {
         String axisName = stepExpr.substring(0,axisSeparator);
         if (axisName.equals("step")) {
            axis = Axis.Step;
         } else if (axisName.equals("port")) {
            axis = Axis.Port;
         } else if (axisName.equals("parameter")) {
            axis = Axis.Parameter;
         } else {
            throw new ParseError("Unknown axis name "+axisName);
         }
         nameString = stepExpr.substring(axisSeparator+2);
      }
      Reference ref = new Reference(axis,nameString);
      if (slash>0) {
         String subexpr = expr.substring(slash+1);
         if (subexpr.length()==0) {
            throw new ParseError("No step after '/'.");
         }
         if (axis==Axis.Port || axis==Axis.Parameter) {
            throw new ParseError("No step is allowed after a port or parameter step.");
         }
         if (defaultAxis==Axis.Step) {
            defaultAxis = Axis.Port;
         }
         ref.child = parse(defaultAxis,subexpr);
      }
      return ref;
   }
    */
   
}
